package cn.otrue.patienthealthmanager.utils;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import cn.otrue.patienthealthmanager.HealthApplication;
import cn.otrue.patienthealthmanager.activity.BaseActivity;
import cn.otrue.patienthealthmanager.bean.PatientModel;
import cn.otrue.patienthealthmanager.fragment.BaseFragment;
import cn.otrue.patienthealthmanager.xmpp.XmppTool;

/**
 * 常量类
 * 
 * @author G
 * 
 */
public class MyConst {

	public static int width = 0;
	public static int height = 0;

	public static final String packageName = "cn.otrue.patienthealthmanager";
	/** 开始页等待时间 */
	public static int TIME = 1000;
	/** 用户token */
	public static String TOKEN = "";
	/** 个人信息实体类 */
	public static PatientModel PATIENT = new PatientModel();
	// public static int NUM = 0;
	public static String POWD = "";
	/** 当前用户 */
	public static String IN_USER = "";
	/** 用户aliasAccount */
	private static String ACCOUNT = "";

	/** 用户头像 */
	public static String HEADIMG = "";
	/** 百度推送channelid */
	public static String CHANNELID = "";
	/** 当前Activity */
	public static BaseActivity activity = null;
	/** 当前Fragment */
	public static BaseFragment fragment = null;

	/** 系统配置文件的命名 */
	public static final String CONFIG_SP_NAME = "catt_config";
	public static final int maxuploadbytes = 5 * 1024 * 1024; // 最大上传5M
	/** 版本标识 */
	public static final String VERSION_ID = "version_id";

	/** 指标编号（1饭前血糖 ） */
	public static final int SUGAR_BE = 1;
	/** 指标编号（2饭后血糖 ） */
	public static final int SUGAR_AF = 2;
	/** 指标编号（ 3高压 ） */
	public static final int PRESSURE_HIGH = 3;
	/** 指标编号（ 4低压 ） */
	public static final int PRESSURE_LOW = 4;
	/** 指标编号（ 5心率） */
	public static final int PLUS = 5;

	public static final int PRESSURE_H = 0;
	public static final int PRESSURE_L = 1;
	public static final int SUGAR_B = 0;
	public static final int SUGAR_A = 1;

	/** 血糖 */
	public static final int SUGAR = 1;
	/** 血压和心率 */
	public static final int PLUS_PRESSURE = 2;
	/** 其他病症 */
	public static final int OTHER = 3;
	/** 其他病症 */
	public static final int OTHER_INFO = 4;

	public static final int DRUG_RESULT = 15;
	public static final int PLUS_RESULT = 16;

	public static final int DRUG = 3;

	// //////健康计划////////////
	/** 关闭 */
	public static final int OFF_PLAN = 0;
	/** 开启 */
	public static final int OPEN_PLAN = 1;
	/** 过期 */
	public static final int OUT_PLAN = 2;
	/** 空白计划 */
	public static final int BLANK_PLAN = 1;
	/** 修改（添加）计划 */
	public static final int CHANGE_PLAN = 2;
	/** 展示计划 */
	public static final int SHOW_PLAN = 3;

	public static final String[] WEEK = { "周一", "周二", "周三", "周四", "周五", "周六",
			"周日" };
	public static final String SYSTEM_MONITOR = "9999";
	public static final String AUTHORITY = "cn.catt.healthmanager.db.HealthDataProvider";
	public static final String TABLE_DATA = "tbData";
	public static final Uri HEALTH_DATA_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_DATA);

	/** 图表查看默认显示的天数 */
	public static final int CHART_DAYS = 6;

	/** 健康数据表-是否正常列 */
	public static final String COLUMN_ISNORMAL = "is_normal";

	// ///////////////////////////////////////////////
	public static final String DENG_LU = "用户名不能为空";
	public static final String YONGHUMING = "用户名不正确";
	public static final String MIMA = "密码不能为空";
	// //////////////////////////////////聊天////////////////////////////////////////////////
	// /////////文件类型////////////////
	public static final int FILE_AAC = 1;
	public static final int FILE_PNG = 2;
	public static final int FILE_TXT = 3;
	// /////////////聊天类型////////////
	public static final int CHAT_IN = 1;
	public static final int CHAT_OUT = 2;
	// /////////////步骤类型////////////
	public static final int SAVE_FILE_START = 1;
	public static final int SAVE_FILE_SUCCESS = 2;
	// //////////////聊天记录类型////////////
	public static final int History_TYPE_FREE = 1;
	public static final int History_TYPE_UNFREE = 2;
	// ///////////////////////////////////图片pageType///////////////////////////////////////////
	/** 欢迎页 */
	public static final int PAGETYPE_WELCOME = 0;
	/** 引导页 */
	public static final int PAGETYPE_GUIDE = 1;
	/** 轮播图 */
	public static final int PAGETYPE_CAROUSEL = 2;

	// ////////////////////////////////////////计划类型////////////////////////////////////////////////////
	/** 关闭 */
	public static final int Open_Type_close = -1;
	/** 开启 */
	public static final int Open_Type_open = 0;
	/** 开启+闹铃 */
	public static final int Open_Type_clock = 1;

	// /////////////////////////////////////////医生列表///////////////////////////////////////////
	public static final int ALL_DOCTOR = 1;
	public static final int ATTENTION_DOCTOR = 2;

	// 消息中心中的typeid
	public static final int TYPEID_P_attention_D = 100;// 患者关注医生申请
	public static final int TYPEID_D_respond_P = 101;// 患者回复医生关注
	public static final int TYPEID_questionnaire = 300;// 问卷调整
	public static final int TYPEID_D_invite_P = 201;// 医生邀请患者关注
	public static final int TYPEID_SYSTEM = 999;// 系统消息
	public static final int TYPEID_P_respond_D = 102;// 患者回应医生邀请
	public static final int TYPEID_D_respond_P_attention = 200;// 医生回应患者关注
	public static final int FREE_ASK = 400;// 免费询问
	public static final int FREE_REPLY = 500;// 免费问回复
	public static final int SUGAR_PLUS = 900;// 免费问回复
	// 消息中心是否查看了
	public static final int ISLOOK_SURE = 1;
	public static final int ISLOOK_NO = 0;
	// 消息中心是否处理了
	public static final int ISDETAIL_SURE = 1;
	public static final int ISDETAIL_NO = 2;

	public static int isLoginXmpp = 0;

	public static String TEMPACCOUNT = "";

	public static String getACCOUNT() {
		String account = "";
		if (!TEMPACCOUNT.isEmpty()) {
			account = TEMPACCOUNT;
		} else {
			SharedPreferences mySharedPreferences = HealthApplication
					.getInstance().getSharedPreferences("userinfo",
							Activity.MODE_PRIVATE);
			TEMPACCOUNT = account = mySharedPreferences
					.getString("account", "");
			Log.e("xmpp", "isLoginXmpp" + isLoginXmpp);
		}

		if (!TextUtils.isEmpty(account)) {
			isLoginXmpp++;
			if (isLoginXmpp == 1) {
				Log.e("xmpp", "首次登陆");
				Map<String, String> userinfo = MyConst.getUserinfo();

				String password = userinfo.get("password");
				if (!TextUtils.isEmpty(password)) {
					// 关闭现有连接
					XmppTool.closeConnection();
					// 重新登陆
					XmppTool.loginOpenFire(account, password, handler);
				}
			}
		}
		return account;
	}

	private static Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			if (msg.what == 1) {

			} else if (msg.what == 2) {

			}
		};
	};

	public static SharedPreferences mySharedPreferences = HealthApplication
			.getInstance().getSharedPreferences("userinfo",
					Activity.MODE_PRIVATE);

	public static void setACCOUNT(String aCCOUNT) {

		SharedPreferences.Editor editor = mySharedPreferences.edit();
		// 用putString的方法保存数据
		editor.putString("account", aCCOUNT);
		// 提交当前数据
		editor.commit();
		TEMPACCOUNT = aCCOUNT;
	}

	public static void setUserInfo(String password, String loginName) {
		SharedPreferences.Editor editor = mySharedPreferences.edit();
		if (!TextUtils.isEmpty(password)) {
			editor.putString("password", password);
		}
		if (!TextUtils.isEmpty(loginName)) {
			editor.putString("loginName", loginName);
		}
		editor.commit();
	}

	public static Map<String, String> getUserinfo() {
		Map<String, String> userMap = new HashMap<String, String>();
		userMap.put("loginName", mySharedPreferences.getString("loginName", ""));
		userMap.put("password", mySharedPreferences.getString("password", ""));
		return userMap;
	}

}
